package sng.net.http
{
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.utils.ByteArray;
	
	/**
	 *  http请求和接收代理 
	 * @author leonliu
	 * 
	 */	
	public class HttpProxy
	{
		/**
		 *  Url请求
		 */		
		private var _urlReq:URLRequest;
		
		/**
		 * 发送buf
		 */		
		private var _sendBuf:ByteArray;
		
		/**
		 * net客户端
		 */		
		private var _netClient:NetClient;
		
		/**
		 * 发送线程池 
		 */		
		private var _threadsPool:ThreadsPool;
		
		public function HttpProxy(client:NetClient)
		{
			
			_netClient = client;
			
			_urlReq = new URLRequest();
			
			_sendBuf = new ByteArray();
			
			_threadsPool = new ThreadsPool();
			
			//
			initUrlReq();
			
			
		}
		
		private function initUrlReq():void
		{
			// 设置超链接IP
			_urlReq.url = "http://10.6.224.127/";//"http://10.6.224.127";//"http://10.6.224.127:8800/";//:8800/crossdomain.xml
			_urlReq.method = URLRequestMethod.POST;
			_urlReq.contentType = "application/octet-stream";
			
		}
		
		/**
		 * 发送消息  
		 * @param msg
		 * @param onRecv
		 * @return 
		 * 
		 */		
		public function sendMsg(buf:ByteArray, onRecv:Function):uint
		{
			// 发送数据
			if(!buf)
			{
				trace("sendMsg msg == null");
				return NetEventDef.NET_EVENT_DATA;
			}
			
			// 设置数据
			_urlReq.data = buf;
			
			return __sendMsg(onRecv);
			
		}
		
		
		
		
		private function __sendMsg(onRecv:Function):uint
		{
			//
			var tread:SendThread = _threadsPool.getThread();
			
			if(!tread)
			{
				trace("当前没有可用的发送数据线程");
				
				// 清空数据
				_urlReq.data = null;
				
				return NetEventDef.NET_EVENT_THREAD;
			}
			else
			{
				tread.sendReq(_urlReq, onRecv);
				
				// 清空数据
				_urlReq.data = null;
				
				return NetEventDef.NET_EVENT_OK;
			}
			
		}
		
	}
}